unit U_Funcoes;

interface

uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, AdvOfficeStatusBar,
  AdvOfficeStatusBarStylers, Vcl.ImgList, Vcl.Imaging.jpeg, Vcl.ExtCtrls,
  System.Actions, Vcl.ActnList, Vcl.PlatformDefaultStyleActnCtrls, Vcl.ActnMan,
  Vcl.Ribbon, Vcl.RibbonLunaStyleActnCtrls, Vcl.ActnCtrls, Vcl.ToolWin,
  Vcl.ActnMenus, Vcl.RibbonActnMenus, Vcl.Imaging.pngimage, JvExExtCtrls,
  JvImage, Vcl.StdCtrls, Vcl.DBCtrls, JvDBImage;

procedure AbreForm(aClasseForm: TComponentClass; var aForm);
function ValidCPF(sCPF: string): string;
function iif(lTest: Boolean; vExpr1, vExpr2: Variant): Variant;
function VersaoEXE: String;

implementation

procedure AbreForm(aClasseForm: TComponentClass; var aForm);
begin
  Application.CreateForm(aClasseForm, aForm);
  try
    TForm(aForm).ShowModal;
  finally
    FreeAndNil(TForm(aForm));
  end;
end;

function ValidCPF(sCPF: string): string;
var
  sDigs, sVal: string;
  iSTot, iSTot2: integer;
  i: integer;
begin
  Result := '---';
  sCPF := Trim(sCPF);
  iSTot := 0;
  iSTot2 := 0;
  if Length(sCPF) = 11 then
  begin
    for i := 9 downto 1 do
    begin
      iSTot := iSTot + StrToInt(Copy(sCPF, i, 1)) * (11 - i);
      iSTot2 := iSTot2 + StrToInt(Copy(sCPF, i, 1)) * (12 - i);
    end;
    iSTot := iSTot mod 11;
    sDigs := sDigs + IntToStr(iif(iSTot < 2, 0, 11 - iSTot));

    iSTot2 := iSTot2 + 2 * StrToInt(sDigs);
    iSTot2 := iSTot2 mod 11;
    sDigs := sDigs + IntToStr(iif(iSTot2 < 2, 0, 11 - iSTot2));

    sVal := Copy(sCPF, 10, 2);
    Result := iif(sDigs = sVal, '', sDigs);
  end
end;

function iif(lTest: Boolean; vExpr1, vExpr2: Variant): Variant;
begin
  if lTest then
    Result := vExpr1
  else
    Result := vExpr2
end;

Function VersaoEXE: String;
type
  PFFI = ^vs_FixedFileInfo;
var
  F: PFFI;
  Handle: Dword;
  Len: Longint;
  Data: Pchar;
  Buffer: Pointer;
  Tamanho: Dword;
  Parquivo: Pchar;
  Arquivo: String;
begin
  Arquivo := Application.ExeName;
  Parquivo := StrAlloc(Length(Arquivo) + 1);
  StrPcopy(Parquivo, Arquivo);
  Len := GetFileVersionInfoSize(Parquivo, Handle);
  Result := '';
  if Len > 0 then
  begin
    Data := StrAlloc(Len + 1);
    if GetFileVersionInfo(Parquivo, Handle, Len, Data) then
    begin
      VerQueryValue(Data, '\', Buffer, Tamanho);
      F := PFFI(Buffer);
      Result := Format('%d.%d.%d.%d', [HiWord(F^.dwFileVersionMs),
        LoWord(F^.dwFileVersionMs), HiWord(F^.dwFileVersionLs),
        LoWord(F^.dwFileVersionLs)]);
    end;
    StrDispose(Data);
  end;
  StrDispose(Parquivo);
end;

end.
